
package com.joysuch.wwyt.bp.controller;

import com.joysuch.wwyt.bp.bean.facility.BpProductionFacilityTypeBean;
import com.joysuch.wwyt.bp.bean.facility.BpProductionFacilityTypeDto;
import com.joysuch.wwyt.bp.entity.BpProductionFacilityType;
import com.joysuch.wwyt.bp.service.BpProductionFacilityTypeService;
import com.joysuch.wwyt.core.common.ajax.ResultBean;
import com.joysuch.wwyt.core.service.SimpleSelectListService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/bp/production-type")
@Api(tags = "设备设施管理-生产装置类型")
public class BpProductionFacilityTypeController {

    @Autowired
    private BpProductionFacilityTypeService bpProductionFacilityTypeService;
    @Autowired
    private SimpleSelectListService simpleSelectListService;

    @GetMapping("list")
    public ResultBean list() {
        List<BpProductionFacilityType> list = bpProductionFacilityTypeService.list();
        return ResultBean.wrapSuccessfulResult(list);
    }

    @GetMapping("tree")
    public ResultBean tree() {
        List<BpProductionFacilityTypeDto> list = bpProductionFacilityTypeService.tree();
        return ResultBean.wrapSuccessfulResult(list);
    }

    @PostMapping("add")
    public ResultBean add(@RequestBody BpProductionFacilityTypeBean bean) {
        bean.setNumber(0);
        bpProductionFacilityTypeService.add(bean);
        return ResultBean.defaultSuccessResult();
    }

    @PostMapping("edit")
    public ResultBean update(@RequestBody BpProductionFacilityTypeBean bean) {
        bpProductionFacilityTypeService.edit(bean);
        return ResultBean.defaultSuccessResult();
    }

    @PostMapping("save")
    @ApiOperation("新增/修改")
    public ResultBean save(@RequestBody @Valid BpProductionFacilityTypeBean bean) {
        if (bean.getId() == null) {
            bean.setNumber(0);
            bpProductionFacilityTypeService.add(bean);
        } else {
            bpProductionFacilityTypeService.edit(bean);
        }
        return ResultBean.defaultSuccessResult();
    }

    @PostMapping("getById")
    public ResultBean getById(Long id) {
        return ResultBean.wrapSuccessfulResult(bpProductionFacilityTypeService.getById(id));
    }

    @GetMapping("simple-list")
    public ResultBean getPage() {
        return ResultBean.wrapSuccessfulResult(simpleSelectListService.list(BpProductionFacilityType.class, "name"));
    }

    @GetMapping("delete")
    public ResultBean delete(Long id) {
        bpProductionFacilityTypeService.delete(id);
        return ResultBean.defaultSuccessResult();
    }

}
